/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package com.android1.amarena2d.actions.base;

import com.android1.amarena2d.actions.interval.DelayTime;
import com.android1.amarena2d.nodes.behavior.ActionTarget;


public class Parallel extends IntervalAction {

    FiniteTimeAction[] actions;

    public static Parallel $(FiniteTimeAction... actions) {
        return new Parallel(findMaxDuration(actions), actions);
    }

    protected Parallel(float duration, FiniteTimeAction... actions) {
        super(duration);
        this.actions = actions;
        for (int i = 0; i < actions.length; i++) {
            actions[i] = populateDelay(actions[i]);
        }
    }


    @Override
    public void start(ActionTarget aTarget) {
        super.start(aTarget);
        for (int i = 0; i < actions.length; i++) {
            FiniteTimeAction action = actions[i];
            action.start(aTarget);
        }
    }

    @Override
    public void stop() {
        for (int i = 0; i < actions.length; i++) {
            FiniteTimeAction action = actions[i];
            action.stop();
        }
        super.stop();
    }

    @Override
    public void update(float time) {
        for (int i = 0; i < actions.length; i++) {
            FiniteTimeAction action = actions[i];
            if (action.duration >= time) {
                action.update(time);
            }
        }
    }

//    @Override
//    public void finished(ActionTarget actionTarget) {
//        for (int i = 0; i < actions.length; i++) {
//            actions[i].finished(actionTarget);
//        }
//        super.finished(actionTarget);
//    }

    @Override
    public IntervalAction reverse() {
        FiniteTimeAction[] reverseActions = new FiniteTimeAction[actions.length];
        int index = actions.length;
        for (int i = 0; i < actions.length; i++) {
            FiniteTimeAction action = actions[i];
            reverseActions[--index] = action.reverse();
        }

        return Parallel.$(reverseActions);
    }

    @Override
    public IntervalAction copy() {
        FiniteTimeAction[] copyActions = new FiniteTimeAction[actions.length];
        for (int i = 0; i < actions.length; i++) {
            FiniteTimeAction action = actions[i];
            copyActions[i] = action.reverse();
        }
        return Parallel.$(copyActions);
    }


    private FiniteTimeAction populateDelay(FiniteTimeAction action) {
        if (action.duration < this.duration) {
            return Sequence.$(action, DelayTime.$(this.duration - action.duration));
        } else
            return action;
    }

    private static float findMaxDuration(FiniteTimeAction... actions) {
        float max = 0;
        for (int i = 0; i < actions.length; i++) {
            FiniteTimeAction finiteTimeAction = actions[i];
            if (finiteTimeAction.duration > max)
                max = finiteTimeAction.duration;
        }
        return max;
    }
}
